package com.cloud.pole.msg.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ace.cache.api.CacheAPI;
import com.cloud.pole.common.constant.CacheConstant;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.msg.service.SmsSendService;
import com.cloud.pole.msg.service.VerifyCodeService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/verifyCode/front")
@Slf4j
public class VerifyCodeController {
	
	@Autowired
    private SmsSendService smsSendService;
	
	@Autowired
    private VerifyCodeService verifyCodeService;
	
	@Autowired
    private CacheAPI cacheAPI;


	/**
	 * 获取验证码
	 * @param mobile
	 * @param verifyCode
	 * @param templateKey
	 * @return
	 */
	@RequestMapping(value = "/getVerifyCode")
    @ResponseBody
    public ApiRestResponse<Boolean> getVerifyCode(
    		@RequestParam(defaultValue = "") String mobileArea,
    		@RequestParam(defaultValue = "") String mobile,
    		@RequestParam(defaultValue = "") String templateKey) throws Exception {
		try {
			ParamException.err(StringUtils.isBlank(mobileArea), "mobileArea不能为空");
			ParamException.err(StringUtils.isBlank(mobile), "mobile不能为空");
			ParamException.err(StringUtils.isBlank(templateKey), "templateKey不能为空");
	
			boolean flag = verifyCodeService.getVerifyCode(mobileArea, mobile, templateKey);
			if(flag) {
				  return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, flag);
			}
			else {
				return ApiRestResponse.fail("获取验证码失败", null);
			}
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	
	/**
	 * 检查验证短信验证码
	 * @param mobile
	 * @param verifyCode
	 * @param templateKey
	 * @return
	 */
	@RequestMapping(value = "/checkVerifyCode")
    @ResponseBody
    public ApiRestResponse checkVerifyCode(String mobile, String verifyCode, String templateKey){
		if(StringUtils.isBlank(mobile)
				||StringUtils.isBlank(verifyCode)
				||StringUtils.isBlank(templateKey)) {
			return ApiRestResponse.fail("参数缺失", null);
		}
		String msgVerifyCodeKey = CacheConstant.SERVICE_MSG_VERIFY_CODE_KEY + ":" +templateKey+ ":" + mobile;
		String confirmVerifyCode = cacheAPI.get(msgVerifyCodeKey);
		if(StringUtils.isNoneBlank(confirmVerifyCode) && confirmVerifyCode.equals(verifyCode)) {
			return ApiRestResponse.success("成功", null);
		}else {
			return ApiRestResponse.fail("失败", null);
		}
    }
	
}
